/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aic.lists;

import aic.beans.Item;
import aic.misc.DebugPrint;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ShippedList {

    private static List<Item> shippedList = Collections.synchronizedList(new LinkedList<Item>());

    /**
     * Prints out a list of all Items within "shippedList"
     */
    public static void printList() {
        Iterator<Item> it = shippedList.iterator();

        while (it.hasNext()) {
            System.out.println(">>list value (shippedList): " + it.next());
        }
    }

    /**
     * This method adds an Item to the "shippedList". If the Item already exists
     * there their quantities will be added
     *
     * @param item Item object
     * @return true if the operation was successful
     */
    public static boolean add(Item item) {
        DebugPrint.println("Entering add");
        try {
            if (shippedList.contains(item)) {
                DebugPrint.println("Item has already been inserted into shippedList: " + item);
                int index = shippedList.indexOf(item);
                Item temp = shippedList.get(index);
                int newQuantity = temp.getQuantity() + item.getQuantity();
                DebugPrint.println("-> Adding quantity to following item: " + temp);
                temp.setQuantity(newQuantity);
                DebugPrint.println(".. done. New item: " + temp + "");
                return true;
            } else {
                DebugPrint.println("Adding Item: " + item);
                shippedList.add(item);
                DebugPrint.println(".. done adding Item");
                return true;
            }
        } catch (Exception e) {
            return false;
        }
    }
}
